package com.moleq.service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.MixMatch_detail;
import com.moleq.models.PriceSetting;


public class MixMatchSettingService {
	private DatabaseHelper DBHelper;
	private String errorString;
	private Context mContext;
	public static final int ALL = 0;
	public static final int UPC = 1;
	public static final int Department = 2;
	public static final int SubDept = 3;
	public static final int Vendor = 4;
	
	public MixMatchSettingService(Context context) {
		this.DBHelper = new DatabaseHelper(context);
		mContext = context;
	}

	public MixMatchSettingService(){

	}
	
	public List<MixMatch_detail> getmmdetaillist(){
		
		List<MixMatch_detail> obj = new ArrayList<MixMatch_detail>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select * from t_mix_match_setting order by ms_no",null);

		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int ms_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.MS_NO));
			String desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.DESCRIPTION));
			String sPriceMultiple = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PRICE_MULTIPLE));
			String sPrice  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PRICE));
			String sStartdate  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.START_DATE));
			String sStarttime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.START_TIME));
			String sEnddate  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.END_DATE));
			String sEndtime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.END_TIME));
			String sPackagepricemultiple  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PACKAGEPRICE_MULTIPLE));
			String sPackageprice  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PACKAGE_PRICE));
			String sLimitedqtyamount  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.LIMITEDQTY_AMOUNT));
			String sLimitedqtyprice  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.LIMITEDQTY_PRICE));
			String sDiscountamount  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.DISCOUNT_AMOUNT));
			String sDiscountpercent  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.DISCOUNT_PERCENT));
			String sDiscntLink  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.DISCOUNT_LINK));
			String sPriceLvl = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PRICE_LVL));
			
			obj.add(new MixMatch_detail(ms_no,desc,sPriceMultiple,sPrice,sStartdate, sStarttime, sEnddate,
					sEndtime, sPackagepricemultiple, sPackageprice,sLimitedqtyamount,sLimitedqtyprice,sDiscountamount,sDiscountpercent,sDiscntLink,sPriceLvl));
		}
		cursor.close();
		return obj;
	}
	
	public MixMatch_detail FindByNO(String ls_mm){
		
		MixMatch_detail obj = null;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = DBHelper.rawQuery("select * from t_mix_match_setting where ms_no = "+ls_mm,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int ms_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.MS_NO));
			String desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.DESCRIPTION));
			String sPriceMultiple = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PRICE_MULTIPLE));
			String sPrice  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PRICE));
			String sStartdate  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.START_DATE));
			String sStarttime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.START_TIME));
			String sEnddate  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.END_DATE));
			String sEndtime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.END_TIME));
			String sPackagepricemultiple  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PACKAGEPRICE_MULTIPLE));
			String sPackageprice  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PACKAGE_PRICE));
			String sLimitedqtyamount  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.LIMITEDQTY_AMOUNT));
			String sLimitedqtyprice  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.LIMITEDQTY_PRICE));
			String sDiscountamount  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.DISCOUNT_AMOUNT));
			String sDiscountpercent  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.DISCOUNT_PERCENT));
			String sDiscntLink  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.DISCOUNT_LINK));
			String sPriceLvl = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PRICE_LVL));
			
			obj = new MixMatch_detail(ms_no,desc,sPriceMultiple,sPrice,sStartdate, sStarttime, sEnddate,
					sEndtime, sPackagepricemultiple, sPackageprice,sLimitedqtyamount,sLimitedqtyprice,sDiscountamount,sDiscountpercent,sDiscntLink,sPriceLvl);
					
		}
		cursor.close();
		return obj;
	}
	
	
	public PriceSetting getitemprice(String ls_mm,String upc){
		
		PriceSetting obj = null;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = DBHelper.rawQuery("select * from t_item_setting as a left join t_mix_match as b on a.F01 = b.F01 left join t_mix_match_setting as c on c.ms_no = b.mm_no where c.ms_no="
									+ls_mm+" and a.F01 = '"+upc+"'", null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			
			String scurrentqty = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.CURRENT_PRICE_QTY));
			String scurrentprice = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemSetting.UNIT_PRICE));
			String sPriceMultiple = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.PRICE_MULTIPLE));
			String sPrice = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.PRICE));
			String sStartdate = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.STAR_TDATE));
			String sStarttime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.STAR_TTIME));
			String sEnddate = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.END_DATE));
			String sEndtime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.END_TIME));
			String sPackagepricemultiple = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.PACKAGEPRICE_MULTIPLE));
			String sPackageprice = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.PACKAGE_PRICE));
			String sLimitedqtyamount = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.LIMITEDQTY_AMOUNT));
			String sLimitedqtyprice = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.LIMITEDQTY_PRICE));
			String sDiscountamount = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.DISCOUNT_AMOUNT));
			String sDiscountpercent = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.DISCOUNT_PERCENT));
			String sDiscountLink = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PriceSetting.DISCOUNT_LINK));
			String sPriceLvl = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PRICE_LVL));

			obj = new PriceSetting(null, sPriceMultiple, sPrice, sStartdate, sStarttime, sEnddate,
					sEndtime, sPackagepricemultiple, sPackageprice,sLimitedqtyamount,sLimitedqtyprice,sDiscountamount,sDiscountpercent
					,null,scurrentqty,scurrentprice,null,null,sDiscountLink,sPriceLvl);
		}

		cursor.close();
		return obj;
	}
	
	public MixMatch_detail getMixMatchDetaile(String ls_mm){
		
		MixMatch_detail obj = null;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = DBHelper.rawQuery("select * from t_mix_match_setting where ms_no= " + ls_mm, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int ms_no = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.MS_NO));
			String ms_desc = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.DESCRIPTION));
			String sPriceMultiple = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PRICE_MULTIPLE));
			String sPrice = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PRICE));
			String sStartdate = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.START_DATE));
			String sStarttime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.START_TIME));
			String sEnddate = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.END_DATE));
			String sEndtime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.END_TIME));
			String sPackagepricemultiple = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PACKAGEPRICE_MULTIPLE));
			String sPackageprice = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PACKAGE_PRICE));
			String sLimitedqtyamount = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.LIMITEDQTY_AMOUNT));
			String sLimitedqtyprice = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.LIMITEDQTY_PRICE));
			String sDiscountamount = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.DISCOUNT_AMOUNT));
			String sDiscountpercent = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.DISCOUNT_PERCENT));
			String sDiscountLink = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.DISCOUNT_LINK));
			String sPriceLvl = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.PRICE_LVL));
			String createTime= cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.CREATE_TIME));
			String updateTime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MixMatchSetting.UPDATE_TIME));
			
			obj = new MixMatch_detail(ms_no,ms_desc, sPriceMultiple, sPrice, sStartdate, sStarttime, sEnddate,
					sEndtime, sPackagepricemultiple, sPackageprice,sLimitedqtyamount,sLimitedqtyprice,sDiscountamount,sDiscountpercent
					,createTime,updateTime,sDiscountLink,sPriceLvl);
		}

		cursor.close();
		return obj;
	}
	
	public boolean insertMmdetail(MixMatch_detail item){
		boolean check = true;
//		if (CheckUsers(user.getUs_id()) < 0 ){return -1;}
//		
//		if(!user.getUs_confirm_pwd().equals(user.getUs_pwd()) ){return -2;}
		
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "insert into t_mix_match_setting values ";
		sql += "("+item.getMs_no()+",'"+item.getMs_desc()+"',"+item.getPriceMultiple()+","+item.getPrice()+","+getdatetime(item.getStartdate())+
				  ","+gettime(item.getStarttime())+","+getdatetime(item.getEnddate())+","+gettime(item.getEndtime())+"," + item.getPackagepricemultiple()+","
		  		+ item.getPackageprice()+","+item.getLimitedqtyamount()+","+item.getLimitedqtyprice()+","+item.getDiscountamount()+","+item.getDiscountpercent()
		  		+",strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'),"+item.getDiscnt_link()+")";
		  
		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		} 
		return check;
		
		
	}
	
	public String getItemname(String upc){
		
		String sql = "",ls_name = null;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		sql = "select F02 from t_item_setting where F01  = '" + upc+"'";
		
		Cursor cursor =DBHelper.rawQuery(sql, null);
		if(cursor == null){
//			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return ls_name;
		}
		if(cursor.moveToNext()){
			ls_name = cursor.getString(0);
			cursor.close();
			return ls_name;
	} 
		cursor.close();
		return ls_name;
	
}


	public boolean DeleteMmdetail(String ms_no ){
		boolean check = true;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "Delete from t_mix_match_setting " ;
		String ls_where = "where ms_no = "+ms_no ;
		sql = sql+ls_where ;
	
		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		} 
		return check;
	}
	
	public boolean updateeMmdetail(MixMatch_detail item){
		boolean check = true;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "update t_mix_match_setting set ";
		  sql +=  "ms_desc  = '" + item.getMs_desc() + "'," +	
				  "F135 = " + item.getPriceMultiple() + "," +
					"F136 = " + item.getPrice() + "," +
					"F137  = " + getdatetime(item.getStartdate()) + "," +
					"F144   = " + gettime(item.getStarttime()) + "," +
					"F138  = " + getdatetime(item.getEnddate()) + ","+
					"F145  = " + gettime(item.getEndtime()) + ","+
					"F143  = " + item.getPackagepricemultiple() + ","+
					"F139  = " + item.getPackageprice() + ","+
					"F147  = " + item.getLimitedqtyamount() + ","+
					"F148  = " + item.getLimitedqtyprice() + ","+
					"F111  = " + item.getDiscountamount() + ","+
					"F112  = " + item.getDiscountpercent() + ","+
					"ms_discnt_link = " + item.getDiscnt_link()+ ","+
					"update_datetime = strftime('%Y%m%d%H%M%S','now')" ;
			
			  sql += " where ms_no   = "+ item.getMs_no();

			  errorString = DBHelper.execSQL(sql);

				if (errorString.length() > 0) {
					check = false;
				} 
				return check;

	}
	
	public Boolean checkitemm(String no){
		boolean lb = false;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select ms_no  from t_mix_match_setting where ms_no  = "+no;
		Cursor cursor = DBHelper.rawQuery(sql, null);
		
		if(cursor == null){
//			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return lb;
		}
		
		if (cursor.moveToNext()) {
			lb = true;
		}
		
		cursor.close();
		return lb;
	}
	
	public int getmaxno(){
		
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select Max(ms_no) from t_mix_match_setting";
		int i = 1;
		Cursor cursor = DBHelper.rawQuery(sql, null);
		
		if(cursor == null){
//			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return -1;
		}
		
		if (cursor.moveToNext()) {
			i = cursor.getInt(0) +1;
		}
		
		cursor.close();
		return i;
	}
	
	 public String getdatetime(String date){
		  
		  	Date curDate = null;
		  	String str;
			SimpleDateFormat  formatter  =  new  SimpleDateFormat("yyyyMMdd");
			DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
			// Date curDate =(Date)date;
			try {
				if (date == null) {
					 return null;
				}
				curDate = format1.parse(date);
				
			}catch(ParseException e) {
				e.printStackTrace();
			}
			if (curDate == null) {
				// str = null;
				 return null;
			}else{
				 str = formatter.format(curDate);
			}
			
			return"'"+str+"'";
		};
		
	  public String gettime(String time){
		  String str;
		  if (time == null) {
			//str = null;
			return null;
		}else
			{
			    str = time.replace(":","");
			}
			
			return "'"+str+"'";
		};

}

